//
//  HomeViewController.m
//  RefreshDemo
//
//  Created by UniHan_iMac on 17/4/11.
//  Copyright © 2017年 UniHan_iMac. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import <SVPullToRefresh.h>
#import <MJRefresh.h>

@interface HomeViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _rowCount;
}
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    _rowCount = 2;
//    [self pullToRefresh];
    [self mjRefresh];
}

#pragma mark SVPullToRefresh刷新
- (void)pullToRefresh
{
    __weak typeof(self) weakSelf = self;
    // 下拉刷新
    [_homeTableView addPullToRefreshWithActionHandler:^{
        [weakSelf beginRefresh];
    }];
    
    // 上拉加载
    [_homeTableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf beginLoad];
    }];
    [_homeTableView.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateTriggered];
    [_homeTableView.pullToRefreshView setTitle:@"刷新完毕" forState:SVPullToRefreshStateStopped];
    [_homeTableView.pullToRefreshView setTitle:@"不要命加载中..." forState:SVPullToRefreshStateLoading];
}
- (void)beginRefresh
{
    [_homeTableView beginUpdates];
    [_homeTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
    _rowCount = _rowCount + 1;
    [_homeTableView reloadData];
    [_homeTableView endUpdates];
    [_homeTableView.pullToRefreshView stopAnimating];
}
- (void)beginLoad
{
    [_homeTableView beginUpdates];
    [_homeTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_rowCount - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
    _rowCount = _rowCount + 1;
    [_homeTableView reloadData];
    [_homeTableView endUpdates];
    [_homeTableView.infiniteScrollingView stopAnimating];
}

#pragma mark MJRefresh刷新
- (void)mjRefresh
{
    _homeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)) , dispatch_get_main_queue(), ^{
            _rowCount++;
            [_homeTableView reloadData];
            [_homeTableView.mj_header endRefreshing];
        });
    }];
    
    _homeTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)) , dispatch_get_main_queue(), ^{
            _rowCount++;
            [_homeTableView reloadData];
            [_homeTableView.mj_footer endRefreshing];
        });
    }];
}

#pragma mark 表的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
    if(cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"HomeTableViewCell" owner:nil options:nil][0];
    }
    cell.homeLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
